import {View, Button} from '@tarojs/components';
import Taro from '@tarojs/taro';
import {request, getAppCode} from "../../utils/index";
import './unionidModal.scss';


class UnionidModal extends Taro.Component {
    static options = {
        addGlobalClass: true
    };
    config = {
        component: true,
        componentGenerics: {
            UnionidModal: true
        }
    };

    state = {
        getSessionkey: '' //获取nu ni id所需要的getSessionkey
    };

    componentWillMount() {
        //获取unionId
        // getAppCode().then(code => {
        //     request({
        //         url: 'weiXinLoginByCode',
        //         data: {
        //             weiXinLoginRequest: code,
        //         }
        //     }).then(res => {
        //         this.setState({
        //             getSessionkey: res.sessionKey, //获取nu ni id所需要的getSessionkey
        //         })
        //     })
        // })
    }

    //取消获取nu ni id
    unGetUserInfo = () => {
        if(this.props.onCancel){
            this.props.onCancel();
        }
    };

    //获取nu ni id
    getUserInfo = (e) => {
        console.log(e);
        const {getSessionkey} = this.state;
        if (e.detail && e.detail.iv) {
            const params = {
                url: '/MiniPro/weiXinSaveLoginUser',
                data:{
                    encryptedData: e.detail.encryptedData,
                    iv: e.detail.iv,
                    user: e.detail.userInfo
                }
            };
            request(params).then(res => {
                if (res) {
                    if(res.code == 200) {
                        this.props.onCancel(1);
                    }
                } else {
                    this.props.onCancel();
                }
            })
        }
    };

    getUserInfoProfile = () => {
        wx.getUserProfile({
            desc: '授权您的个人信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (e) => {
                const params = {
                    url: '/MiniPro/weiXinSaveLoginUser',
                    data:{
                        encryptedData: e.encryptedData,
                        iv: e.iv,
                        user: e.userInfo
                    }
                };
                request(params).then(res => {
                    if (res) {
                        if(res.code == 200) {
                            this.props.onCancel(1);
                        }
                    } else {
                        this.props.onCancel();
                    }
                })
            }
        })
    };

    render() {
        return (
            <View className="have-union">
                <View className="union-wrap">
                    <View className="union-text">为了给您提供更优质的服务，现需您允许微信授权。</View>
                    <View className="union-btn">
                        <View className='at-row btn-mg flex-lr'>
                            <View className='at-col flex-center'>
                                <Button className="close-btn" onClick={this.unGetUserInfo}>下次再说</Button>
                            </View>
                            <View className='at-col flex-center'>
                                {/*<Button openType="getUserInfo" className="sumbit-btn"  onGetuserinfo={this.getUserInfo}>去授权</Button>*/}
                                <Button className="sumbit-btn"  onClick={this.getUserInfoProfile}>去授权</Button>
                            </View>
                        </View>
                    </View>
                </View>
            </View>
        )
    }
}

export default UnionidModal;

